package org.leetcode.easy.leetcode404;

import org.leetcode.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

public class Solution
{


    public int sumOfLeftLeaves2(TreeNode root)
    {
        Queue<TreeNode> queue = new LinkedList<>();

        queue.add(root);

        int sum = 0;

        while (!queue.isEmpty())
        {
            int size = queue.size();


            while (size>0)
            {
                TreeNode tempNode = queue.poll();

                if (tempNode.left!=null)
                {
                    queue.add(tempNode.left);

                    if (tempNode.left.left==null && tempNode.left.right==null)
                    {
                        sum=sum+tempNode.left.val;
                    }
                }

                if (tempNode.right!=null)
                {
                    queue.add(tempNode.right);
                }
                size--;
            }

        }

        return sum;
    }


    public int sumOfLeftLeaves(TreeNode root) {

        int midValue =0;

        int leftValue = sumOfLeftLeaves(root.left);
        int rightValue = sumOfLeftLeaves(root.right);

        if (root.left!=null && root.left.left==null && root.left.right==null)
        {
            midValue=root.left.val;
        }

        int sum = midValue+leftValue+rightValue;

        return sum;
    }

    private int sumCount(TreeNode node,int sum)
    {
        if (node==null)
        {
            return sum;
        }

        sum=sumCount(node.left,sum);

        sum = sumCount(node.right, sum);


        return sum;
    }
}
